﻿using System.Collections;
using System.Configuration;
using System.Text;
using MPMovieCatalog.Util;

namespace MPMovieCatalog.Configuration.Sections
{
    public sealed class TextExportSection : PropertySection
    {
        [ConfigurationProperty("fileName")]
        private PropertyElement m_fileName
        {
            get
            {
                PropertyElement fileName = (PropertyElement)base["fileName"];
                return fileName;
            }

            set
            {
                base["fileName"] = value;
            }
        }

        [ConfigurationProperty("encoding")]
        private PropertyElement m_encoding
        {
            get
            {
                PropertyElement encoding = (PropertyElement)base["encoding"];
                return encoding;
            }

            set
            {
                base["encoding"] = value;
            }
        }

        [ConfigurationProperty("movies")]
        private PropertyElement m_movies
        {
            get
            {
                PropertyElement movies = (PropertyElement)base["movies"];
                return movies;
            }

            set
            {
                base["movies"] = value;
            }
        }

        [ConfigurationProperty("fieldNamesIncluded")]
        private PropertyElement m_fieldNamesIncluded
        {
            get
            {
                PropertyElement fieldNamesIncluded = (PropertyElement)base["fieldNamesIncluded"];
                return fieldNamesIncluded;
            }

            set
            {
                base["fieldNamesIncluded"] = value;
            }
        }

        [ConfigurationProperty("delimiter")]
        private PropertyElement m_delimiter
        {
            get
            {
                PropertyElement delimiter = (PropertyElement)base["delimiter"];
                return delimiter;
            }

            set
            {
                base["delimiter"] = value;
            }
        }

        [ConfigurationProperty("qualifier")]
        private PropertyElement m_qualifier
        {
            get
            {
                PropertyElement qualifier = (PropertyElement)base["qualifier"];
                return qualifier;
            }

            set
            {
                base["qualifier"] = value;
            }
        }

        [ConfigurationProperty("lineBreaks")]
        private PropertyElement m_lineBreaks
        {
            get
            {
                PropertyElement lineBreaks = (PropertyElement)base["lineBreaks"];
                return lineBreaks;
            }

            set
            {
                base["lineBreaks"] = value;
            }
        }

        [ConfigurationProperty("fields")]
        private PropertyElement m_fields
        {
            get
            {
                PropertyElement fields = (PropertyElement)base["fields"];
                return fields;
            }

            set
            {
                base["fields"] = value;
            }
        }

        public string FileName
        {
            get
            {
                return m_fileName.Value;
            }
            set
            {
                SetProperty("fileName", value);
            }
        }

        public string Encoding
        {
            get
            {
                return m_encoding.Value;
            }
            set
            {
                SetProperty("encoding", value);
            }
        }

        public int Movies
        {
            get
            {
                return m_movies.ValueAsInt;
            }
            set
            {
                SetProperty("movies", value.ToString());
            }
        }

        public bool IsFieldNamesIncluded
        {
            get
            {
                return m_fieldNamesIncluded.ValueAsBoolean;
            }
            set
            {
                SetProperty("fieldNamesIncluded", value.ToString().ToLower());
            }
        }

        public int Delimeter
        {
            get
            {
                return m_delimiter.ValueAsInt;
            }
            set
            {
                SetProperty("delimiter", value.ToString());
            }
        }

        public int Qualifier
        {
            get
            {
                return m_qualifier.ValueAsInt;
            }
            set
            {
                SetProperty("qualifier", value.ToString());
            }
        }

        public int LineBreaks
        {
            get
            {
                return m_lineBreaks.ValueAsInt;
            }
            set
            {
                SetProperty("lineBreaks", value.ToString());
            }
        }

        public ArrayList Fields
        {
            get
            {
                ArrayList fields = new ArrayList();
                foreach (string name in m_fields.Value.Split(';'))
                {
                    Field field = MPMovieCatalog.Util.Fields.FindFieldByName(name);
                    if (field != null)
                    {
                        fields.Add(field);
                    }
                }

                return fields;
            }
            set
            {
                StringBuilder sb = new StringBuilder();
                foreach (Field field in value)
                {
                    if (sb.Length > 0)
                    {
                        sb.Append(";");
                    }
                    sb.Append(field.Name);
                }
                SetProperty("fields", sb.ToString());
            }
        }
    }
}
